﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2008 Socansoft.com 版权所有

// 创建描述: SocanCode代码生成器自动创建于 2008-9-8 11:32:58
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using DALFactory;
using Model;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace BLL
{
    /// <summary>
    /// 业务逻辑类 ORDERDETAIL
    /// </summary>
    public class ORDERDETAIL
    {
        private readonly IDAL.IORDERDETAIL dal = DataAccess.CreateORDERDETAIL();
        public ORDERDETAIL()
        { }

        #region  成员方法
        /// <summary>
        /// 增加一条数据


        /// </summary>
        public string Add(Model.ORDERDETAIL model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 增加多条数据
        /// </summary>
        public void Add(List<Model.ORDERDETAIL> l)
        {
            foreach (Model.ORDERDETAIL model in l)
                dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据


        /// </summary>
        public bool Update(Model.ORDERDETAIL model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 更新多条数据
        /// </summary>
        public void Update(List<Model.ORDERDETAIL> l)
        {
            foreach (Model.ORDERDETAIL model in l)
                dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据


        /// </summary>
        public bool Delete(string OD_ID)
        {
            return dal.Delete(OD_ID);
        }

        /// <summary>
        /// 删除多条数据
        /// </summary>
        public void Delete(List<string> l)
        {
            foreach (string OD_ID in l)
                dal.Delete(OD_ID);
        }

        /// <summary>
        /// 是否存在该记录


        /// </summary>
        public bool Exists(string OD_ID)
        {
            return dal.Exists(OD_ID);
        }

        /// <summary>
        /// 得到一个对象实体


        /// </summary>
        public Model.ORDERDETAIL GetModel(string OD_ID)
        {
            return dal.GetModel(OD_ID);
        }

        /// <summary>
        /// 获得泛型数据列表
        /// </summary>
        public List<Model.ORDERDETAIL> GetAllList()
        {
            return dal.GetList() as List<Model.ORDERDETAIL>;
        }

        /// <summary>
        /// 分页获取泛型数据列表,不建议直接使用此方法,请根据业务逻辑重写
        /// </summary>
        public List<Model.ORDERDETAIL> GetList(int pageSize, int pageIndex, string fldSort, bool Sort, string strCondition, out int pageCount, out int Counts)
        {
            return dal.GetList(pageSize, pageIndex, fldSort, Sort, strCondition, out pageCount, out Counts) as List<Model.ORDERDETAIL>;
        }

        /// <summary>
        /// 根据订单号获取订单明细

        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public DataSet GetOrderDetail(string where)
        {
            return dal.GetOrderDetail(where);
        }
        public bool UpdateOrderDetailAndOrderMain(string[] sqlCommandArray)
        {
            return new SQLServerDAL.ORDERDETAIL().UpdateOrderDetailAndOrderMain(sqlCommandArray);


        }

        /// <summary>
        /// 更新订单明细和订单主表，同时生成新订单和增加可用库存
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="deletedOrderDetailsId"></param>
        /// <param name="newOrderDetails"></param>
        /// <param name="toUpdateOrderDetails"></param>
        /// <returns></returns>
        public bool UpdateOrderDetailAndOrderMain(bool isCreateNewOrder, string orderId, string deletedOrderDetailsId, List<Model.ORDERDETAIL> newOrderDetails, List<Model.ORDERDETAIL> toUpdateOrderDetails, string userCode, string ckdm, string kwdm, string tranerCode)
        {
            #region 生成差异订单
            if (isCreateNewOrder)
            {
                #region 当有商品被删除时并且订单数量被修改时
                if (deletedOrderDetailsId != "" && newOrderDetails.Count >= 0)
                {
                    StringBuilder strSql = new StringBuilder();

                    Model.ORDERDOC oldOrder = new BLL.ORDERDOC().GetModel(orderId);
                    string userName = (new BLL.USERS().GetModel(userCode)).USERS_NAME;

                    decimal oldOrderWeight = 0.00M;
                    decimal newOlderWeight = 0.00M;

                    foreach (Model.ORDERDETAIL temp in toUpdateOrderDetails)
                    {
                        if (temp.OD_QTY > 0)
                        {
                            strSql.Append(" update ORDERDETAIL set OD_QTY=");
                            strSql.Append(temp.OD_QTY);
                            strSql.Append(", OD_AAMT=");
                            strSql.Append(temp.OD_AAMT);
                            strSql.Append(", OD_SAMT=");
                            strSql.Append(temp.OD_SAMT);
                            strSql.Append(", OD_NOTE='");
                            strSql.Append(temp.OD_NOTE);
                            strSql.Append("', OD_ITG=");
                            strSql.Append(temp.OD_ITG);
                            strSql.Append(", OD_WEIGHT=");
                            strSql.Append(temp.OD_WEIGHT);
                            strSql.Append(" where OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("' and OD_DETAILID='");
                            strSql.Append(temp.OD_DETAILID);
                            strSql.Append("'|");

                            if (oldOrder != null)
                            {
                                strSql.Append(" insert into OrderNote(ON_BILLNO,ON_USERCODE,ON_TIME,ON_NOTETYPE,ON_NOTE) values('");
                                strSql.Append(oldOrder.ORDER_ID);
                                strSql.Append("','");
                                strSql.Append(userCode);
                                strSql.Append("','");
                                strSql.Append(DateTime.Now);
                                strSql.Append("','");
                                strSql.Append(oldOrder.ORDER_STATETYPECODE);
                                strSql.Append("','修改人:");
                                strSql.Append(userName);
                                strSql.Append(" 修改时间:");
                                strSql.Append(DateTime.Now);
                                strSql.Append(" 商品代码:");
                                strSql.Append(temp.OD_CMDCODE);
                                strSql.Append(" 商品名称:");
                                strSql.Append((new BLL.SHANGPIN().GetModel(temp.OD_CMDCODE)).SPMC);
                                strSql.Append(" 商品颜色:");
                                strSql.Append(new BLL.GUIGE1().GetMC(temp.OD_COLORCODE));
                                strSql.Append(" 商品尺码:");
                                strSql.Append(new BLL.GUIGE2().GetMC(temp.OD_SIZECODE));
                                strSql.Append(" 修改后数量:");
                                strSql.Append(temp.OD_QTY);
                                strSql.Append("')|");
                            }

                            oldOrderWeight += temp.OD_WEIGHT * temp.OD_QTY;
                        }
                        else
                        {
                            strSql.Append(" delete from ORDERDETAIL where OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("' and OD_DETAILID='");
                            strSql.Append(temp.OD_DETAILID);
                            strSql.Append("'|");

                            //oldOrderWeight += temp.OD_WEIGHT * temp.OD_QTY;
                        }
                    }

                    if (oldOrder != null)
                    {
                        string s = new SQLServerDAL.ORDERDOC().GetNewOrderID();
                        BLL.Common.ADDLSH("400101");
                        string newOrderId = new SQLServerDAL.ORDERDOC().GetNewOrderID();
                        BLL.Common.ADDLSH("400101");
                        if (newOrderId != null && newOrderId != "")
                        {

                            strSql.Append(" insert into ORDERDOC([ORDER_ID],[ORDER_AID],[ORDER_CUSTOMERCODE],[ORDER_OFCODE],[ORDER_DTCODE],[ORDER_TRANERCODE],");
                            strSql.Append("[ORDER_TRANCOST],[ORDER_COSTNATURE],[ORDER_GETER],[ORDER_GETERPHONE1],[ORDER_GETERPHONE2],[ORDER_GETADDRESS],");
                            strSql.Append("[ORDER_GETPOSTCODE],[ORDER_CREATER],[ORDER_CREATEDATE],[ORDER_STATETYPECODE],[ORDER_FIELD1],[ORDER_FIELD2],");
                            strSql.Append("[ORDER_ITGBUY],[ORDER_ITG],[ORDER_NINV],[ORDER_INVTITLE],[ORDER_OFFERTYPE],[ORDER_OFFERID],[ORDER_NOTE],[ORDER_APAY],[order_Province],[order_city],[order_area])VALUES('");
                            strSql.Append(newOrderId);
                            strSql.Append("','");
                            strSql.Append(orderId);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_CUSTOMERCODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_OFCODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_DTCODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_TRANERCODE);
                            strSql.Append("',");
                            strSql.Append(oldOrder.ORDER_TRANCOST);
                            strSql.Append(",'");
                            strSql.Append(oldOrder.ORDER_COSTNATURE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETER);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETERPHONE1);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETERPHONE2);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETADDRESS);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETPOSTCODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_CREATER);
                            strSql.Append("','");
                            strSql.Append(DateTime.Now);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_STATETYPECODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_FIELD1);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_FIELD2);
                            strSql.Append("',");
                            strSql.Append(oldOrder.ORDER_ITGBUY);
                            strSql.Append(",");
                            strSql.Append(oldOrder.ORDER_ITG);
                            strSql.Append(",");
                            strSql.Append(oldOrder.ORDER_NINV);
                            strSql.Append(",'");
                            strSql.Append(oldOrder.ORDER_INVTITLE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_OFFERTYPE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_OFFERID);
                            strSql.Append("','差异订单',");
                            strSql.Append(oldOrder.ORDER_APAY);
                            strSql.Append(",'");
                            strSql.Append(oldOrder.ORDER_PROVINCE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_CITY);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_AREA);
                            strSql.Append("')|");

                            char[] ch = new char[] { ',' };
                            char[] ch1 = new char[] { '\'' };
                            string[] deletedOrderDetailsIdArray = deletedOrderDetailsId.TrimEnd(ch).Split(',');
                            for (int i = 0; i < deletedOrderDetailsIdArray.Length; i++)
                            {
                                strSql.Append("update ORDERDETAIL set OD_ID='");
                                strSql.Append(newOrderId);
                                strSql.Append("' where OD_ID='");
                                strSql.Append(orderId);
                                strSql.Append("' and OD_DETAILID=");
                                strSql.Append(deletedOrderDetailsIdArray[i]);
                                strSql.Append(" |");



                                DataSet dstWeightAndQty = GetQtyAndWeightByOrderIdAndDetailId(orderId, deletedOrderDetailsIdArray[i].TrimEnd(ch1).TrimStart(ch1));
                                if (dstWeightAndQty.Tables[0].Rows.Count > 0)
                                {
                                    newOlderWeight += Convert.ToDecimal(dstWeightAndQty.Tables[0].Rows[0]["od_qty"]) * Convert.ToDecimal(dstWeightAndQty.Tables[0].Rows[0]["od_weight"]);
                                }

                                if (oldOrder != null)
                                {
                                    DataSet dst = GetOrderDetailById(orderId, deletedOrderDetailsIdArray[i].TrimEnd(ch1).TrimStart(ch1));
                                    if (dst.Tables[0].Rows.Count > 0)
                                    {

                                        strSql.Append(" insert into OrderNote(ON_BILLNO,ON_USERCODE,ON_TIME,ON_NOTETYPE,ON_NOTE) values('");
                                        strSql.Append(oldOrder.ORDER_ID);
                                        strSql.Append("','");
                                        strSql.Append(userCode);
                                        strSql.Append("','");
                                        strSql.Append(DateTime.Now);
                                        strSql.Append("','");
                                        strSql.Append(oldOrder.ORDER_STATETYPECODE);
                                        strSql.Append("','修改人:");
                                        strSql.Append(userName);
                                        strSql.Append(" 修改时间:");
                                        strSql.Append(DateTime.Now);
                                        strSql.Append(" 商品代码:");
                                        strSql.Append(dst.Tables[0].Rows[0]["OD_CMDCODE"].ToString());
                                        strSql.Append(" 商品名称:");
                                        strSql.Append(dst.Tables[0].Rows[0]["SPMC"].ToString());
                                        strSql.Append(" 商品颜色:");
                                        strSql.Append(dst.Tables[0].Rows[0]["COLOR"].ToString());
                                        strSql.Append(" 商品尺码:");
                                        strSql.Append(dst.Tables[0].Rows[0]["SIZE"].ToString());
                                        strSql.Append(" 修改后数量:");
                                        strSql.Append(0);
                                        strSql.Append("')|");
                                    }
                                }
                            }

                            foreach (Model.ORDERDETAIL temp in newOrderDetails)
                            {
                                strSql.Append("INSERT INTO ORDERDETAIL([OD_ID],[OD_DETAILID],[OD_CMDCODE],[OD_COLORCODE],[OD_SIZECODE],");
                                strSql.Append("[OD_QTY],[OD_SENDQTY],[OD_SPRICE],[OD_DISC],[OD_APRICE],[OD_SAMT],[OD_AAMT],[OD_NOTE],[OD_FIELD1],");
                                strSql.Append("[OD_FIELD2],[OD_SPPRICE],[OD_GIFT],[OD_ITGBUY],[OD_ITGPRICE],[OD_ITG],[OD_ADDITG],[OD_WEIGHT],");
                                strSql.Append("[OD_OFFER],[OD_OFFERTYPE],[OD_OFFERID],[OD_OFFERNO])VALUES('");
                                strSql.Append(newOrderId);
                                strSql.Append("','");
                                strSql.Append(temp.OD_DETAILID);
                                strSql.Append("','");
                                strSql.Append(temp.OD_CMDCODE);
                                strSql.Append("','");
                                strSql.Append(temp.OD_COLORCODE);
                                strSql.Append("','");
                                strSql.Append(temp.OD_SIZECODE);
                                strSql.Append("',");
                                strSql.Append(temp.OD_QTY);
                                strSql.Append(",");
                                strSql.Append(temp.OD_SENDQTY);
                                strSql.Append(",");
                                strSql.Append(temp.OD_SPPRICE);
                                strSql.Append(",");
                                strSql.Append(temp.OD_DISC);
                                strSql.Append(",");
                                strSql.Append(temp.OD_APRICE);
                                strSql.Append(",");
                                strSql.Append(temp.OD_SAMT);
                                strSql.Append(",");
                                strSql.Append(temp.OD_AAMT);
                                strSql.Append(",");
                                strSql.Append("null,'");
                                strSql.Append(temp.OD_FIELD1);
                                strSql.Append("','");
                                strSql.Append(temp.OD_FIELD2);
                                strSql.Append("',");
                                strSql.Append(temp.OD_SPPRICE);
                                strSql.Append(",");
                                strSql.Append(temp.OD_GIFT);
                                strSql.Append(",");
                                strSql.Append(temp.OD_ITGBUY);
                                strSql.Append(",");
                                strSql.Append(temp.OD_ITGPRICE);
                                strSql.Append(",");
                                strSql.Append(temp.OD_ITG);
                                strSql.Append(",");
                                strSql.Append(temp.OD_ADDITG);
                                strSql.Append(",");
                                strSql.Append(temp.OD_WEIGHT);
                                strSql.Append(",");
                                strSql.Append(temp.OD_OFFER);
                                strSql.Append(",'");
                                strSql.Append(temp.OD_OFFERTYPE);
                                strSql.Append("','");
                                strSql.Append(temp.OD_OFFERID);
                                strSql.Append("','");
                                strSql.Append(temp.OD_OFFERNO);
                                strSql.Append("')| ");

                                newOlderWeight += temp.OD_QTY * temp.OD_WEIGHT;
                            }

                            decimal amountAddToBYZD13 = 0;
                            foreach (Model.ORDERDETAIL temp in newOrderDetails)
                            {
                                strSql.Append(" update SPKCB set SL3=SL3+");
                                strSql.Append(temp.OD_QTY);
                                strSql.Append(" where CKDM='");
                                strSql.Append(ckdm);
                                strSql.Append("' and KWDM='");
                                strSql.Append(kwdm);
                                strSql.Append("' and SPDM='");
                                strSql.Append(temp.OD_CMDCODE);
                                strSql.Append("' and GG1DM='");
                                strSql.Append(temp.OD_COLORCODE);
                                strSql.Append("' and GG2DM='");
                                strSql.Append(temp.OD_SIZECODE);
                                strSql.Append("'|");

                                amountAddToBYZD13 += temp.OD_AAMT;
                            }

                            int pageCount, recordCount;
                            List<Model.ORDERDETAIL> orderDetails = new BLL.ORDERDETAIL().GetList(100, 1, " OD_DETAILID ", false, " and OD_ID='" + orderId + "' and OD_DETAILID in(" + deletedOrderDetailsId.TrimEnd(ch) + ")", out pageCount, out recordCount);
                            if (orderDetails != null && orderDetails.Count > 0)
                            {
                                foreach (Model.ORDERDETAIL temp in orderDetails)
                                {
                                    strSql.Append(" update SPKCB set SL3=SL3+");
                                    strSql.Append(temp.OD_QTY);
                                    strSql.Append(" where CKDM='");
                                    strSql.Append(ckdm);
                                    strSql.Append("' and KWDM='");
                                    strSql.Append(kwdm);
                                    strSql.Append("' and SPDM='");
                                    strSql.Append(temp.OD_CMDCODE);
                                    strSql.Append("' and GG1DM='");
                                    strSql.Append(temp.OD_COLORCODE);
                                    strSql.Append("' and GG2DM='");
                                    strSql.Append(temp.OD_SIZECODE);
                                    strSql.Append("'|");


                                    amountAddToBYZD13 += temp.OD_AAMT;
                                }
                            }

                            if (oldOrder != null && oldOrder.ORDER_DTCODE == "003")
                            {
                                strSql.Append(new BLL.V_VIPSET().BuildUpdateVipSetSqlString(amountAddToBYZD13, 0, 0, oldOrder.ORDER_CUSTOMERCODE));
                            }

                            decimal oldOrderTranCost = new BLL.TRANERCOST().GetTranCost(oldOrder.ORDER_TRANERCODE, oldOrder.ORDER_CITY, oldOrderWeight);
                            decimal newOrderTranCost = new BLL.TRANERCOST().GetTranCost(oldOrder.ORDER_TRANERCODE, oldOrder.ORDER_CITY, newOlderWeight);


                            strSql.Append("update orderdoc set ORDER_TRANCOST=");
                            strSql.Append(oldOrderTranCost);
                            strSql.Append(",ORDER_QTY=(SELECT isnull(SUM(OD_QTY),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'),ORDER_AMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'),ORDER_TAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("')+");
                            strSql.Append(oldOrderTranCost);
                            strSql.Append(",ORDER_AAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("')-ORDER_OFFERAMT+");
                            strSql.Append(oldOrderTranCost);
                            strSql.Append(",ORDER_ITG=(SELECT isnull(SUM(OD_ITG),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'),ORDER_WEIGHT=(SELECT isnull(SUM(OD_QTY*OD_WEIGHT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'),ORDER_TRANERCODE='");
                            strSql.Append(tranerCode);
                            strSql.Append("' where order_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'|");

                            strSql.Append("update orderdoc set ORDER_TRANCOST=");
                            strSql.Append(newOrderTranCost);
                            strSql.Append(",ORDER_QTY=(SELECT isnull(SUM(OD_QTY),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("'),ORDER_AMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("'),ORDER_TAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("')+");
                            strSql.Append(newOrderTranCost);
                            strSql.Append(",ORDER_AAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("')-ORDER_OFFERAMT+");
                            strSql.Append(newOrderTranCost);
                            strSql.Append(",ORDER_ITG=(SELECT isnull(SUM(OD_ITG),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("'),ORDER_WEIGHT=(SELECT isnull(SUM(OD_QTY*OD_WEIGHT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("') where order_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("'|");

                        }

                    }
                    string stringSql = strSql.ToString();
                    char[] charToTrim = new char[] { '|' };
                    string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);
                    return new SQLServerDAL.ORDERDETAIL().UpdateOrderDetailAndOrderMain(sqlCommandArray);

                }
                #endregion
                #region 当没有商品被删除并且订单数量被修改时
                else if (deletedOrderDetailsId == "" && newOrderDetails.Count > 0)
                {
                    StringBuilder strSql = new StringBuilder();

                    Model.ORDERDOC oldOrder = new BLL.ORDERDOC().GetModel(orderId);
                    string userName = (new BLL.USERS().GetModel(userCode)).USERS_NAME;

                    decimal oldOrderWeight = 0.00M;
                    decimal newOlderWeight = 0.00M;

                    foreach (Model.ORDERDETAIL temp in toUpdateOrderDetails)
                    {
                        if (temp.OD_QTY > 0)
                        {
                            strSql.Append(" update ORDERDETAIL set OD_QTY=");
                            strSql.Append(temp.OD_QTY);
                            strSql.Append(", OD_AAMT=");
                            strSql.Append(temp.OD_AAMT);
                            strSql.Append(", OD_SAMT=");
                            strSql.Append(temp.OD_SAMT);
                            strSql.Append(", OD_NOTE='");
                            strSql.Append(temp.OD_NOTE);
                            strSql.Append("', OD_ITG=");
                            strSql.Append(temp.OD_ITG);
                            strSql.Append(", OD_WEIGHT=");
                            strSql.Append(temp.OD_WEIGHT);
                            strSql.Append(" where OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("' and OD_DETAILID='");
                            strSql.Append(temp.OD_DETAILID);
                            strSql.Append("'|");

                            if (oldOrder != null)
                            {
                                strSql.Append(" insert into OrderNote(ON_BILLNO,ON_USERCODE,ON_TIME,ON_NOTETYPE,ON_NOTE) values('");
                                strSql.Append(oldOrder.ORDER_ID);
                                strSql.Append("','");
                                strSql.Append(userCode);
                                strSql.Append("','");
                                strSql.Append(DateTime.Now);
                                strSql.Append("','");
                                strSql.Append(oldOrder.ORDER_STATETYPECODE);
                                strSql.Append("','修改人:");
                                strSql.Append(userName);
                                strSql.Append(" 修改时间:");
                                strSql.Append(DateTime.Now);
                                strSql.Append(" 商品代码:");
                                strSql.Append(temp.OD_CMDCODE);
                                strSql.Append(" 商品名称:");
                                strSql.Append((new BLL.SHANGPIN().GetModel(temp.OD_CMDCODE)).SPMC);
                                strSql.Append(" 商品颜色:");
                                strSql.Append(new BLL.GUIGE1().GetMC(temp.OD_COLORCODE));
                                strSql.Append(" 商品尺码:");
                                strSql.Append(new BLL.GUIGE2().GetMC(temp.OD_SIZECODE));
                                strSql.Append(" 修改后数量:");
                                strSql.Append(temp.OD_QTY);
                                strSql.Append("')|");
                            }

                            oldOrderWeight += temp.OD_WEIGHT * temp.OD_QTY;
                        }
                        else
                        {
                            strSql.Append(" delete from ORDERDETAIL where OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("' and OD_DETAILID='");
                            strSql.Append(temp.OD_DETAILID);
                            strSql.Append("'|");

                            //oldOrderWeight += temp.OD_WEIGHT * temp.OD_QTY;
                        }
                    }

                    if (oldOrder != null)
                    {
                        string s = new SQLServerDAL.ORDERDOC().GetNewOrderID();
                        BLL.Common.ADDLSH("400101");
                        string newOrderId = new SQLServerDAL.ORDERDOC().GetNewOrderID();
                        BLL.Common.ADDLSH("400101");
                        if (newOrderId != null && newOrderId != "")
                        {
                            strSql.Append(" insert into ORDERDOC([ORDER_ID],[ORDER_AID],[ORDER_CUSTOMERCODE],[ORDER_OFCODE],[ORDER_DTCODE],[ORDER_TRANERCODE],");
                            strSql.Append("[ORDER_TRANCOST],[ORDER_COSTNATURE],[ORDER_GETER],[ORDER_GETERPHONE1],[ORDER_GETERPHONE2],[ORDER_GETADDRESS],");
                            strSql.Append("[ORDER_GETPOSTCODE],[ORDER_CREATER],[ORDER_CREATEDATE],[ORDER_STATETYPECODE],[ORDER_FIELD1],[ORDER_FIELD2],");
                            strSql.Append("[ORDER_ITGBUY],[ORDER_ITG],[ORDER_NINV],[ORDER_INVTITLE],[ORDER_OFFERTYPE],[ORDER_OFFERID],[ORDER_NOTE],[ORDER_APAY],[order_Province],[order_city],[order_area])VALUES('");
                            strSql.Append(newOrderId);
                            strSql.Append("','");
                            strSql.Append(orderId);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_CUSTOMERCODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_OFCODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_DTCODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_TRANERCODE);
                            strSql.Append("',");
                            strSql.Append(oldOrder.ORDER_TRANCOST);
                            strSql.Append(",'");
                            strSql.Append(oldOrder.ORDER_COSTNATURE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETER);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETERPHONE1);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETERPHONE2);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETADDRESS);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETPOSTCODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_CREATER);
                            strSql.Append("','");
                            strSql.Append(DateTime.Now);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_STATETYPECODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_FIELD1);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_FIELD2);
                            strSql.Append("',");
                            strSql.Append(oldOrder.ORDER_ITGBUY);
                            strSql.Append(",");
                            strSql.Append(oldOrder.ORDER_ITG);
                            strSql.Append(",");
                            strSql.Append(oldOrder.ORDER_NINV);
                            strSql.Append(",'");
                            strSql.Append(oldOrder.ORDER_INVTITLE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_OFFERTYPE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_OFFERID);
                            strSql.Append("','差异订单',");
                            strSql.Append(oldOrder.ORDER_APAY);
                            strSql.Append(",'");
                            strSql.Append(oldOrder.ORDER_PROVINCE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_CITY);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_AREA);
                            strSql.Append("')|");


                            foreach (Model.ORDERDETAIL temp in newOrderDetails)
                            {
                                strSql.Append("INSERT INTO ORDERDETAIL([OD_ID],[OD_DETAILID],[OD_CMDCODE],[OD_COLORCODE],[OD_SIZECODE],");
                                strSql.Append("[OD_QTY],[OD_SENDQTY],[OD_SPRICE],[OD_DISC],[OD_APRICE],[OD_SAMT],[OD_AAMT],[OD_NOTE],[OD_FIELD1],");
                                strSql.Append("[OD_FIELD2],[OD_SPPRICE],[OD_GIFT],[OD_ITGBUY],[OD_ITGPRICE],[OD_ITG],[OD_ADDITG],[OD_WEIGHT],");
                                strSql.Append("[OD_OFFER],[OD_OFFERTYPE],[OD_OFFERID],[OD_OFFERNO])VALUES('");
                                strSql.Append(newOrderId);
                                strSql.Append("','");
                                strSql.Append(temp.OD_DETAILID);
                                strSql.Append("','");
                                strSql.Append(temp.OD_CMDCODE);
                                strSql.Append("','");
                                strSql.Append(temp.OD_COLORCODE);
                                strSql.Append("','");
                                strSql.Append(temp.OD_SIZECODE);
                                strSql.Append("',");
                                strSql.Append(temp.OD_QTY);
                                strSql.Append(",");
                                strSql.Append(temp.OD_SENDQTY);
                                strSql.Append(",");
                                strSql.Append(temp.OD_SPRICE);
                                strSql.Append(",");
                                strSql.Append(temp.OD_DISC);
                                strSql.Append(",");
                                strSql.Append(temp.OD_APRICE);
                                strSql.Append(",");
                                strSql.Append(temp.OD_SAMT);
                                strSql.Append(",");
                                strSql.Append(temp.OD_AAMT);
                                strSql.Append(",");
                                strSql.Append("null,'");
                                strSql.Append(temp.OD_FIELD1);
                                strSql.Append("','");
                                strSql.Append(temp.OD_FIELD2);
                                strSql.Append("',");
                                strSql.Append(temp.OD_SPPRICE);
                                strSql.Append(",");
                                strSql.Append(temp.OD_GIFT);
                                strSql.Append(",");
                                strSql.Append(temp.OD_ITGBUY);
                                strSql.Append(",");
                                strSql.Append(temp.OD_ITGPRICE);
                                strSql.Append(",");
                                strSql.Append(temp.OD_ITG);
                                strSql.Append(",");
                                strSql.Append(temp.OD_ADDITG);
                                strSql.Append(",");
                                strSql.Append(temp.OD_WEIGHT);
                                strSql.Append(",");
                                strSql.Append(temp.OD_OFFER);
                                strSql.Append(",'");
                                strSql.Append(temp.OD_OFFERTYPE);
                                strSql.Append("','");
                                strSql.Append(temp.OD_OFFERID);
                                strSql.Append("','");
                                strSql.Append(temp.OD_OFFERNO);
                                strSql.Append("')| ");

                                newOlderWeight += temp.OD_QTY * temp.OD_WEIGHT;
                            }

                            decimal amountAddToBYZD13 = 0;
                            foreach (Model.ORDERDETAIL temp in newOrderDetails)
                            {
                                strSql.Append(" update SPKCB set SL3=SL3+");
                                strSql.Append(temp.OD_QTY);
                                strSql.Append(" where CKDM='");
                                strSql.Append(ckdm);
                                strSql.Append("' and KWDM='");
                                strSql.Append(kwdm);
                                strSql.Append("' and SPDM='");
                                strSql.Append(temp.OD_CMDCODE);
                                strSql.Append("' and GG1DM='");
                                strSql.Append(temp.OD_COLORCODE);
                                strSql.Append("' and GG2DM='");
                                strSql.Append(temp.OD_SIZECODE);
                                strSql.Append("'|");


                                amountAddToBYZD13 += temp.OD_AAMT;
                            }

                            if (oldOrder != null && oldOrder.ORDER_DTCODE == "003")
                            {
                                strSql.Append(new BLL.V_VIPSET().BuildUpdateVipSetSqlString(amountAddToBYZD13, 0, 0, oldOrder.ORDER_CUSTOMERCODE));
                            }

                            decimal oldOrderTranCost = new BLL.TRANERCOST().GetTranCost(oldOrder.ORDER_TRANERCODE, oldOrder.ORDER_CITY, oldOrderWeight);
                            decimal newOrderTranCost = new BLL.TRANERCOST().GetTranCost(oldOrder.ORDER_TRANERCODE, oldOrder.ORDER_CITY, newOlderWeight);


                            strSql.Append("update orderdoc set ORDER_TRANCOST=");
                            strSql.Append(oldOrderTranCost);
                            strSql.Append(",ORDER_QTY=(SELECT isnull(SUM(OD_QTY),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'),ORDER_AMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'),ORDER_TAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("')+");
                            strSql.Append(oldOrderTranCost);
                            strSql.Append(",ORDER_AAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("')-ORDER_OFFERAMT+");
                            strSql.Append(oldOrderTranCost);
                            strSql.Append(",ORDER_ITG=(SELECT isnull(SUM(OD_ITG),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'),ORDER_WEIGHT=(SELECT isnull(SUM(OD_QTY*OD_WEIGHT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'),ORDER_TRANERCODE='");
                            strSql.Append(tranerCode);
                            strSql.Append("' where order_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'|");

                            strSql.Append("update orderdoc set ORDER_TRANCOST=");
                            strSql.Append(newOrderTranCost);
                            strSql.Append(",ORDER_QTY=(SELECT isnull(SUM(OD_QTY),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("'),ORDER_AMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("'),ORDER_TAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("')+");
                            strSql.Append(newOrderTranCost);
                            strSql.Append(",ORDER_AAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("')-ORDER_OFFERAMT+");
                            strSql.Append(newOrderTranCost);
                            strSql.Append(",ORDER_ITG=(SELECT isnull(SUM(OD_ITG),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("'),ORDER_WEIGHT=(SELECT isnull(SUM(OD_QTY*OD_WEIGHT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("') where order_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("'|");
                        }

                    }
                    string stringSql = strSql.ToString();
                    char[] charToTrim = new char[] { '|' };
                    string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);
                    return new SQLServerDAL.ORDERDETAIL().UpdateOrderDetailAndOrderMain(sqlCommandArray);
                }
                #endregion
                #region 当没有商品被删除并且订单数量没有被修改时
                else
                {
                    StringBuilder strSql = new StringBuilder();

                    strSql.Append("update orderdoc set ORDER_QTY=(SELECT isnull(SUM(OD_QTY),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'),ORDER_AMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'),ORDER_TAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("')+ORDER_TRANCOST,ORDER_AAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("')-ORDER_OFFERAMT+ORDER_TRANCOST,");
                    strSql.Append("ORDER_ITG=(SELECT isnull(SUM(OD_ITG),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'),ORDER_WEIGHT=(SELECT isnull(SUM(OD_QTY*OD_WEIGHT),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'),ORDER_TRANERCODE='");
                    strSql.Append(tranerCode);
                    strSql.Append("' where order_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'|");

                    foreach (Model.ORDERDETAIL temp in toUpdateOrderDetails)
                    {
                        strSql.Append(" update ORDERDETAIL set OD_NOTE='");
                        strSql.Append(temp.OD_NOTE);
                        strSql.Append("' where OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("' and OD_DETAILID='");
                        strSql.Append(temp.OD_DETAILID);
                        strSql.Append("'|");
                    }



                    string stringSql = strSql.ToString();
                    char[] charToTrim = new char[] { '|' };
                    string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);
                    return new SQLServerDAL.ORDERDETAIL().UpdateOrderDetailAndOrderMain(sqlCommandArray);
                }
                #endregion
            }
            #endregion
            #region 不生成差异订单

            else
            {
                #region 当有商品被删除时并且订单数量被修改时
                if (deletedOrderDetailsId != "" && newOrderDetails.Count >= 0)
                {
                    StringBuilder strSql = new StringBuilder();

                    Model.ORDERDOC oldOrder = new BLL.ORDERDOC().GetModel(orderId);
                    string userName = (new BLL.USERS().GetModel(userCode)).USERS_NAME;

                    decimal orderWeight = 0.00M;

                    foreach (Model.ORDERDETAIL temp in toUpdateOrderDetails)
                    {
                        if (temp.OD_QTY > 0)
                        {
                            strSql.Append(" update ORDERDETAIL set OD_QTY=");
                            strSql.Append(temp.OD_QTY);
                            strSql.Append(", OD_AAMT=");
                            strSql.Append(temp.OD_AAMT);
                            strSql.Append(", OD_SAMT=");
                            strSql.Append(temp.OD_SAMT);
                            strSql.Append(", OD_NOTE='");
                            strSql.Append(temp.OD_NOTE);
                            strSql.Append("', OD_ITG=");
                            strSql.Append(temp.OD_ITG);
                            strSql.Append(", OD_WEIGHT=");
                            strSql.Append(temp.OD_WEIGHT);
                            strSql.Append(" where OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("' and OD_DETAILID='");
                            strSql.Append(temp.OD_DETAILID);
                            strSql.Append("'|");

                            if (oldOrder != null)
                            {
                                strSql.Append(" insert into OrderNote(ON_BILLNO,ON_USERCODE,ON_TIME,ON_NOTETYPE,ON_NOTE) values('");
                                strSql.Append(oldOrder.ORDER_ID);
                                strSql.Append("','");
                                strSql.Append(userCode);
                                strSql.Append("','");
                                strSql.Append(DateTime.Now);
                                strSql.Append("','");
                                strSql.Append(oldOrder.ORDER_STATETYPECODE);
                                strSql.Append("','修改人:");
                                strSql.Append(userName);
                                strSql.Append(" 修改时间:");
                                strSql.Append(DateTime.Now);
                                strSql.Append(" 商品代码:");
                                strSql.Append(temp.OD_CMDCODE);
                                strSql.Append(" 商品名称:");
                                strSql.Append((new BLL.SHANGPIN().GetModel(temp.OD_CMDCODE)).SPMC);
                                strSql.Append(" 商品颜色:");
                                strSql.Append(new BLL.GUIGE1().GetMC(temp.OD_COLORCODE));
                                strSql.Append(" 商品尺码:");
                                strSql.Append(new BLL.GUIGE2().GetMC(temp.OD_SIZECODE));
                                strSql.Append(" 修改后数量:");
                                strSql.Append(temp.OD_QTY);
                                strSql.Append("')|");
                            }

                            orderWeight += temp.OD_WEIGHT * temp.OD_QTY;
                        }
                        else
                        {
                            strSql.Append(" delete from ORDERDETAIL where OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("' and OD_DETAILID='");
                            strSql.Append(temp.OD_DETAILID);
                            strSql.Append("'|");

                            //orderWeight += temp.OD_WEIGHT * temp.OD_QTY;
                        }
                    }

                    if (oldOrder != null)
                    {
                        char[] ch = new char[] { ',' };
                        char[] ch1 = new char[] { '\'' };
                        string[] deletedOrderDetailsIdArray = deletedOrderDetailsId.TrimEnd(ch).Split(',');
                        for (int i = 0; i < deletedOrderDetailsIdArray.Length; i++)
                        {
                            strSql.Append("delete from ORDERDETAIL where OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("' and OD_DETAILID=");
                            strSql.Append(deletedOrderDetailsIdArray[i]);
                            strSql.Append(" |");



                            if (oldOrder != null)
                            {
                                DataSet dst = GetOrderDetailById(orderId, deletedOrderDetailsIdArray[i].TrimEnd(ch1).TrimStart(ch1));
                                if (dst.Tables[0].Rows.Count > 0)
                                {
                                    strSql.Append(" insert into OrderNote(ON_BILLNO,ON_USERCODE,ON_TIME,ON_NOTETYPE,ON_NOTE) values('");
                                    strSql.Append(oldOrder.ORDER_ID);
                                    strSql.Append("','");
                                    strSql.Append(userCode);
                                    strSql.Append("','");
                                    strSql.Append(DateTime.Now);
                                    strSql.Append("','");
                                    strSql.Append(oldOrder.ORDER_STATETYPECODE);
                                    strSql.Append("','修改人:");
                                    strSql.Append(userName);
                                    strSql.Append(" 修改时间:");
                                    strSql.Append(DateTime.Now);
                                    strSql.Append(" 商品代码:");
                                    strSql.Append(dst.Tables[0].Rows[0]["OD_CMDCODE"].ToString());
                                    strSql.Append(" 商品名称:");
                                    strSql.Append(dst.Tables[0].Rows[0]["SPMC"].ToString());
                                    strSql.Append(" 商品颜色:");
                                    strSql.Append(dst.Tables[0].Rows[0]["COLOR"].ToString());
                                    strSql.Append(" 商品尺码:");
                                    strSql.Append(dst.Tables[0].Rows[0]["SIZE"].ToString());
                                    strSql.Append(" 修改后数量:");
                                    strSql.Append(0);
                                    strSql.Append("')|");
                                }
                            }
                        }


                        decimal amountAddToBYZD13 = 0;
                        foreach (Model.ORDERDETAIL temp in newOrderDetails)
                        {
                            strSql.Append(" update SPKCB set SL3=SL3+");
                            strSql.Append(temp.OD_QTY);
                            strSql.Append(" where CKDM='");
                            strSql.Append(ckdm);
                            strSql.Append("' and KWDM='");
                            strSql.Append(kwdm);
                            strSql.Append("' and SPDM='");
                            strSql.Append(temp.OD_CMDCODE);
                            strSql.Append("' and GG1DM='");
                            strSql.Append(temp.OD_COLORCODE);
                            strSql.Append("' and GG2DM='");
                            strSql.Append(temp.OD_SIZECODE);
                            strSql.Append("'|");


                            amountAddToBYZD13 += temp.OD_AAMT;
                        }

                        int pageCount, recordCount;
                        List<Model.ORDERDETAIL> orderDetails = new BLL.ORDERDETAIL().GetList(100, 1, " OD_DETAILID ", false, " and OD_ID='" + orderId + "' and OD_DETAILID in(" + deletedOrderDetailsId.TrimEnd(ch) + ")", out pageCount, out recordCount);
                        if (orderDetails != null && orderDetails.Count > 0)
                        {
                            foreach (Model.ORDERDETAIL temp in orderDetails)
                            {
                                strSql.Append(" update SPKCB set SL3=SL3+");
                                strSql.Append(temp.OD_QTY);
                                strSql.Append(" where CKDM='");
                                strSql.Append(ckdm);
                                strSql.Append("' and KWDM='");
                                strSql.Append(kwdm);
                                strSql.Append("' and SPDM='");
                                strSql.Append(temp.OD_CMDCODE);
                                strSql.Append("' and GG1DM='");
                                strSql.Append(temp.OD_COLORCODE);
                                strSql.Append("' and GG2DM='");
                                strSql.Append(temp.OD_SIZECODE);
                                strSql.Append("'|");



                                amountAddToBYZD13 += temp.OD_AAMT;
                            }
                        }

                        if (oldOrder != null && oldOrder.ORDER_DTCODE == "003")
                        {
                            strSql.Append(new BLL.V_VIPSET().BuildUpdateVipSetSqlString(amountAddToBYZD13, 0, 0, oldOrder.ORDER_CUSTOMERCODE));
                        }

                        decimal orderTranCost = new BLL.TRANERCOST().GetTranCost(oldOrder.ORDER_TRANERCODE, oldOrder.ORDER_CITY, orderWeight);

                        strSql.Append("update orderdoc set ORDER_TRANCOST=");
                        strSql.Append(orderTranCost);
                        strSql.Append(",ORDER_QTY=(SELECT isnull(SUM(OD_QTY),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'),ORDER_AMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'),ORDER_TAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("')+");
                        strSql.Append(orderTranCost);
                        strSql.Append(",ORDER_AAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("')-ORDER_OFFERAMT+");
                        strSql.Append(orderTranCost);
                        strSql.Append(",ORDER_ITG=(SELECT isnull(SUM(OD_ITG),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'),ORDER_WEIGHT=(SELECT isnull(SUM(OD_QTY*OD_WEIGHT),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'),ORDER_TRANERCODE='");
                        strSql.Append(tranerCode);
                        strSql.Append("' where order_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'|");
                    }
                    string stringSql = strSql.ToString();
                    char[] charToTrim = new char[] { '|' };
                    string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);
                    return new SQLServerDAL.ORDERDETAIL().UpdateOrderDetailAndOrderMain(sqlCommandArray);

                }
                #endregion
                #region 当没有商品被删除并且订单数量被修改时
                else if (deletedOrderDetailsId == "" && newOrderDetails.Count > 0)
                {
                    StringBuilder strSql = new StringBuilder();

                    Model.ORDERDOC oldOrder = new BLL.ORDERDOC().GetModel(orderId);
                    string userName = (new BLL.USERS().GetModel(userCode)).USERS_NAME;

                    decimal orderWeight = 0.00M;

                    foreach (Model.ORDERDETAIL temp in toUpdateOrderDetails)
                    {
                        if (temp.OD_QTY > 0)
                        {
                            strSql.Append(" update ORDERDETAIL set OD_QTY=");
                            strSql.Append(temp.OD_QTY);
                            strSql.Append(", OD_AAMT=");
                            strSql.Append(temp.OD_AAMT);
                            strSql.Append(", OD_SAMT=");
                            strSql.Append(temp.OD_SAMT);
                            strSql.Append(", OD_NOTE='");
                            strSql.Append(temp.OD_NOTE);
                            strSql.Append("', OD_ITG=");
                            strSql.Append(temp.OD_ITG);
                            strSql.Append(", OD_WEIGHT=");
                            strSql.Append(temp.OD_WEIGHT);
                            strSql.Append(" where OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("' and OD_DETAILID='");
                            strSql.Append(temp.OD_DETAILID);
                            strSql.Append("'|");


                            if (oldOrder != null)
                            {
                                strSql.Append(" insert into OrderNote(ON_BILLNO,ON_USERCODE,ON_TIME,ON_NOTETYPE,ON_NOTE) values('");
                                strSql.Append(oldOrder.ORDER_ID);
                                strSql.Append("','");
                                strSql.Append(userCode);
                                strSql.Append("','");
                                strSql.Append(DateTime.Now);
                                strSql.Append("','");
                                strSql.Append(oldOrder.ORDER_STATETYPECODE);
                                strSql.Append("','修改人:");
                                strSql.Append(userName);
                                strSql.Append(" 修改时间:");
                                strSql.Append(DateTime.Now);
                                strSql.Append(" 商品代码:");
                                strSql.Append(temp.OD_CMDCODE);
                                strSql.Append(" 商品名称:");
                                strSql.Append((new BLL.SHANGPIN().GetModel(temp.OD_CMDCODE)).SPMC);
                                strSql.Append(" 商品颜色:");
                                strSql.Append(new BLL.GUIGE1().GetMC(temp.OD_COLORCODE));
                                strSql.Append(" 商品尺码:");
                                strSql.Append(new BLL.GUIGE2().GetMC(temp.OD_SIZECODE));
                                strSql.Append(" 修改后数量:");
                                strSql.Append(temp.OD_QTY);
                                strSql.Append("')|");
                            }

                            orderWeight += temp.OD_WEIGHT * temp.OD_QTY;
                        }
                        else
                        {
                            strSql.Append(" delete from ORDERDETAIL where OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("' and OD_DETAILID='");
                            strSql.Append(temp.OD_DETAILID);
                            strSql.Append("'|");
                        }
                    }

                    if (oldOrder != null)
                    {
                        decimal amountAddToBYZD13 = 0;
                        foreach (Model.ORDERDETAIL temp in newOrderDetails)
                        {
                            strSql.Append(" update SPKCB set SL3=SL3+");
                            strSql.Append(temp.OD_QTY);
                            strSql.Append(" where CKDM='");
                            strSql.Append(ckdm);
                            strSql.Append("' and KWDM='");
                            strSql.Append(kwdm);
                            strSql.Append("' and SPDM='");
                            strSql.Append(temp.OD_CMDCODE);
                            strSql.Append("' and GG1DM='");
                            strSql.Append(temp.OD_COLORCODE);
                            strSql.Append("' and GG2DM='");
                            strSql.Append(temp.OD_SIZECODE);
                            strSql.Append("'|");


                            amountAddToBYZD13 += temp.OD_AAMT;
                        }

                        if (oldOrder != null && oldOrder.ORDER_DTCODE == "003")
                        {
                            strSql.Append(new BLL.V_VIPSET().BuildUpdateVipSetSqlString(amountAddToBYZD13, 0, 0, oldOrder.ORDER_CUSTOMERCODE));
                        }

                        decimal orderTranCost = new BLL.TRANERCOST().GetTranCost(oldOrder.ORDER_TRANERCODE, oldOrder.ORDER_CITY, orderWeight);

                        strSql.Append("update orderdoc set ORDER_TRANCOST=");
                        strSql.Append(orderTranCost);
                        strSql.Append(",ORDER_QTY=(SELECT isnull(SUM(OD_QTY),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'),ORDER_AMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'),ORDER_TAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("')+");
                        strSql.Append(orderTranCost);
                        strSql.Append(",ORDER_AAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("')-ORDER_OFFERAMT+");
                        strSql.Append(orderTranCost);
                        strSql.Append(",ORDER_ITG=(SELECT isnull(SUM(OD_ITG),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'),ORDER_WEIGHT=(SELECT isnull(SUM(OD_QTY*OD_WEIGHT),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'),ORDER_TRANERCODE='");
                        strSql.Append(tranerCode);
                        strSql.Append("' where order_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'|");


                    }
                    string stringSql = strSql.ToString();
                    char[] charToTrim = new char[] { '|' };
                    string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);
                    return new SQLServerDAL.ORDERDETAIL().UpdateOrderDetailAndOrderMain(sqlCommandArray);
                }
                #endregion
                #region 当没有商品被删除并且订单数量没有被修改时
                else
                {
                    StringBuilder strSql = new StringBuilder();

                    strSql.Append("update orderdoc set ORDER_QTY=(SELECT isnull(SUM(OD_QTY),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'),ORDER_AMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'),ORDER_TAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("')+ORDER_TRANCOST,ORDER_AAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("')-ORDER_OFFERAMT+ORDER_TRANCOST,");
                    strSql.Append("ORDER_ITG=(SELECT isnull(SUM(OD_ITG),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'),ORDER_WEIGHT=(SELECT isnull(SUM(OD_QTY*OD_WEIGHT),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'),ORDER_TRANERCODE='");
                    strSql.Append(tranerCode);
                    strSql.Append("' where order_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'|");

                    foreach (Model.ORDERDETAIL temp in toUpdateOrderDetails)
                    {
                        strSql.Append(" update ORDERDETAIL set OD_NOTE='");
                        strSql.Append(temp.OD_NOTE);
                        strSql.Append("' where OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("' and OD_DETAILID='");
                        strSql.Append(temp.OD_DETAILID);
                        strSql.Append("'|");
                    }



                    string stringSql = strSql.ToString();
                    char[] charToTrim = new char[] { '|' };
                    string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);
                    return new SQLServerDAL.ORDERDETAIL().UpdateOrderDetailAndOrderMain(sqlCommandArray);
                }
                #endregion
            }
            #endregion

        }

        #endregion

        /// <summary>
        /// 由订单编号取出商品明细

        /// </summary>
        public DataSet P_Order_GetProductMX(string orderid)
        {
            return new SQLServerDAL.ORDERDETAIL().P_Order_GetProductMX(orderid);
        }
        /// <summary>
        /// 更新订单商品明细数据
        /// </summary>
        public bool P_Order_UpdateProductMX(string orderid, string od_detailid, string color, string size, int sl, decimal zk)
        {
            return new SQLServerDAL.ORDERDETAIL().P_Order_UpdateProductMX(orderid, od_detailid, color, size, sl, zk);
        }
        /// <summary>
        /// 添加订单商品明细数据
        /// </summary>
        public bool P_Order_InsertProductMX(string orderid, string od_cmdcode, string color, string size, int sl, decimal zk)
        {
            return new SQLServerDAL.ORDERDETAIL().P_Order_InsertProductMX(orderid, od_cmdcode, color, size, sl, zk);
        }
        /// <summary>
        /// 删除订单商品明细数据
        /// </summary>
        public bool P_Order_DeleteProductMX(string orderid, string od_detailid)
        {
            return new SQLServerDAL.ORDERDETAIL().P_Order_DeleteProductMX(orderid, od_detailid);
        }

        public DataSet GetOrderDetailById(string orderId, string detailId)
        {
            return new SQLServerDAL.ORDERDETAIL().GetOrderDetailById(orderId, detailId);
        }

        public DataSet GetOrderDetailByOrderId(string orderid)
        {
            return new SQLServerDAL.ORDERDETAIL().GetOrderDetailByOrderId(orderid);
        }

        /// <summary>
        /// 向临时表插入新的行

        /// </summary>
        /// <param name="tbl"></param>
        /// <returns></returns>
        public DataTable AddDataRowToDataTable(DataTable tbl, string productCode, decimal discount, decimal sprice, string spmc, decimal weight, string isPresent, string presentSource, string promotionId, string promotionMode)
        {
            DataRow newRow = tbl.NewRow();
            newRow["od_id"] = "";
            newRow["od_detailid"] = Convert.ToString(tbl.Rows.Count + 1);
            newRow["od_colorcode"] = "";
            newRow["od_sizecode"] = "";
            newRow["od_cmdcode"] = productCode;
            newRow["od_qty"] = 1;
            newRow["od_disc"] = discount;
            newRow["od_sprice"] = sprice;
            newRow["od_aprice"] = sprice * discount;
            newRow["od_samt"] = sprice;
            newRow["od_aamt"] = sprice * discount;
            newRow["spmc"] = spmc;
            newRow["od_weight"] = weight;
            newRow["OD_FIELD1"] = presentSource;
            newRow["OD_FIELD2"] = isPresent;
            newRow["OD_OFFERNO"] = promotionMode;
            newRow["OD_OFFERID"] = promotionId;
            newRow["PromotionOffAmt"] = 0.00M;
            newRow["PromotionIsUseCustomerDiscount"] = 1;
            tbl.Rows.Add(newRow);
            return tbl;
        }

        public DataTable SubDataRowFromDataTable(DataTable tbl, int rowIndex)
        {
            tbl.Rows.RemoveAt(rowIndex);
            return tbl;
        }

        public DataSet GetQtyAndWeightByOrderIdAndDetailId(string orderId, string detailId)
        {
            return new SQLServerDAL.ORDERDETAIL().GetQtyAndWeightByOrderIdAndDetailId(orderId, detailId);
        }

        public DataSet GetProductSalesStatistics(string type, string tableString, string insertString, string selectOrderDocString, string orderDocLeftJoinString,
            string orderDocGroupByString, string selectReturnDocString, string returnDocLeftJoinString, string returnDocGroupByString, string orderDocWhere, string returnDocWhere)
        {
            return new SQLServerDAL.ORDERDETAIL().GetProductSalesStatistics(type, tableString, insertString, selectOrderDocString, orderDocLeftJoinString,
                orderDocGroupByString, selectReturnDocString, returnDocLeftJoinString, returnDocGroupByString, orderDocWhere, returnDocWhere);
        }
        /// <summary>
        /// 扫描出库，更新订单明细和订单主表，同时生成新订单和增加可用库存
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="deletedOrderDetailsId"></param>
        /// <param name="newOrderDetails"></param>
        /// <param name="toUpdateOrderDetails"></param>
        /// <returns></returns>
        public bool UpdateOrderDetailAndOrderMain(bool isCreateNewOrder, string orderId, string deletedOrderDetailsId, List<Model.ORDERDETAIL> newOrderDetails, List<Model.ORDERDETAIL> toUpdateOrderDetails, string userCode, string ckdm, string kwdm, string tranerCode,string tranerBillCode)
        {
            #region 生成差异订单
            if (isCreateNewOrder)
            {
                #region 当有商品被删除时并且订单数量被修改时
                if (deletedOrderDetailsId != "" && newOrderDetails.Count >= 0)
                {
                    StringBuilder strSql = new StringBuilder();

                    Model.ORDERDOC oldOrder = new BLL.ORDERDOC().GetModel(orderId);
                    string userName = (new BLL.USERS().GetModel(userCode)).USERS_NAME;

                    decimal oldOrderWeight = 0.00M;
                    decimal newOlderWeight = 0.00M;

                    foreach (Model.ORDERDETAIL temp in toUpdateOrderDetails)
                    {
                        if (temp.OD_QTY > 0)
                        {
                            strSql.Append(" update ORDERDETAIL set OD_QTY=");
                            strSql.Append(temp.OD_QTY);
                            strSql.Append(", OD_AAMT=");
                            strSql.Append(temp.OD_AAMT);
                            strSql.Append(", OD_SAMT=");
                            strSql.Append(temp.OD_SAMT);
                            strSql.Append(", OD_NOTE='");
                            strSql.Append(temp.OD_NOTE);
                            strSql.Append("', OD_ITG=");
                            strSql.Append(temp.OD_ITG);
                            strSql.Append(", OD_WEIGHT=");
                            strSql.Append(temp.OD_WEIGHT);
                            strSql.Append(" where OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("' and OD_DETAILID='");
                            strSql.Append(temp.OD_DETAILID);
                            strSql.Append("'|");

                            if (oldOrder != null)
                            {
                                strSql.Append(" insert into OrderNote(ON_BILLNO,ON_USERCODE,ON_TIME,ON_NOTETYPE,ON_NOTE) values('");
                                strSql.Append(oldOrder.ORDER_ID);
                                strSql.Append("','");
                                strSql.Append(userCode);
                                strSql.Append("','");
                                strSql.Append(DateTime.Now);
                                strSql.Append("','");
                                strSql.Append(oldOrder.ORDER_STATETYPECODE);
                                strSql.Append("','修改人:");
                                strSql.Append(userName);
                                strSql.Append(" 修改时间:");
                                strSql.Append(DateTime.Now);
                                strSql.Append(" 商品代码:");
                                strSql.Append(temp.OD_CMDCODE);
                                strSql.Append(" 商品名称:");
                                strSql.Append((new BLL.SHANGPIN().GetModel(temp.OD_CMDCODE)).SPMC);
                                strSql.Append(" 商品颜色:");
                                strSql.Append(new BLL.GUIGE1().GetMC(temp.OD_COLORCODE));
                                strSql.Append(" 商品尺码:");
                                strSql.Append(new BLL.GUIGE2().GetMC(temp.OD_SIZECODE));
                                strSql.Append(" 修改后数量:");
                                strSql.Append(temp.OD_QTY);
                                strSql.Append("')|");
                            }

                            oldOrderWeight += temp.OD_WEIGHT * temp.OD_QTY;
                        }
                        else
                        {
                            strSql.Append(" delete from ORDERDETAIL where OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("' and OD_DETAILID='");
                            strSql.Append(temp.OD_DETAILID);
                            strSql.Append("'|");

                            //oldOrderWeight += temp.OD_WEIGHT * temp.OD_QTY;
                        }
                    }

                    if (oldOrder != null)
                    {
                        string s = new SQLServerDAL.ORDERDOC().GetNewOrderID();
                        BLL.Common.ADDLSH("400101");
                        string newOrderId = new SQLServerDAL.ORDERDOC().GetNewOrderID();
                        BLL.Common.ADDLSH("400101");
                        if (newOrderId != null && newOrderId != "")
                        {

                            strSql.Append(" insert into ORDERDOC([ORDER_ID],[ORDER_AID],[ORDER_CUSTOMERCODE],[ORDER_OFCODE],[ORDER_DTCODE],[ORDER_TRANERCODE],");
                            strSql.Append("[ORDER_TRANCOST],[ORDER_COSTNATURE],[ORDER_GETER],[ORDER_GETERPHONE1],[ORDER_GETERPHONE2],[ORDER_GETADDRESS],");
                            strSql.Append("[ORDER_GETPOSTCODE],[ORDER_CREATER],[ORDER_CREATEDATE],[ORDER_STATETYPECODE],[ORDER_FIELD1],[ORDER_FIELD2],");
                            strSql.Append("[ORDER_ITGBUY],[ORDER_ITG],[ORDER_NINV],[ORDER_INVTITLE],[ORDER_OFFERTYPE],[ORDER_OFFERID],[ORDER_NOTE],[ORDER_APAY],[order_Province],[order_city],[order_area],[ORDER_TRANBILLCODE])VALUES('");
                            strSql.Append(newOrderId);
                            strSql.Append("','");
                            strSql.Append(orderId);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_CUSTOMERCODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_OFCODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_DTCODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_TRANERCODE);
                            strSql.Append("',");
                            strSql.Append(oldOrder.ORDER_TRANCOST);
                            strSql.Append(",'");
                            strSql.Append(oldOrder.ORDER_COSTNATURE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETER);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETERPHONE1);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETERPHONE2);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETADDRESS);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETPOSTCODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_CREATER);
                            strSql.Append("','");
                            strSql.Append(DateTime.Now);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_STATETYPECODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_FIELD1);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_FIELD2);
                            strSql.Append("',");
                            strSql.Append(oldOrder.ORDER_ITGBUY);
                            strSql.Append(",");
                            strSql.Append(oldOrder.ORDER_ITG);
                            strSql.Append(",");
                            strSql.Append(oldOrder.ORDER_NINV);
                            strSql.Append(",'");
                            strSql.Append(oldOrder.ORDER_INVTITLE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_OFFERTYPE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_OFFERID);
                            strSql.Append("','差异订单',");
                            strSql.Append(oldOrder.ORDER_APAY);
                            strSql.Append(",'");
                            strSql.Append(oldOrder.ORDER_PROVINCE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_CITY);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_AREA);
                            strSql.Append("','");
                            strSql.Append(tranerBillCode);
                            strSql.Append("')|");

                            char[] ch = new char[] { ',' };
                            char[] ch1 = new char[] { '\'' };
                            string[] deletedOrderDetailsIdArray = deletedOrderDetailsId.TrimEnd(ch).Split(',');
                            for (int i = 0; i < deletedOrderDetailsIdArray.Length; i++)
                            {
                                strSql.Append("update ORDERDETAIL set OD_ID='");
                                strSql.Append(newOrderId);
                                strSql.Append("' where OD_ID='");
                                strSql.Append(orderId);
                                strSql.Append("' and OD_DETAILID=");
                                strSql.Append(deletedOrderDetailsIdArray[i]);
                                strSql.Append(" |");



                                DataSet dstWeightAndQty = GetQtyAndWeightByOrderIdAndDetailId(orderId, deletedOrderDetailsIdArray[i].TrimEnd(ch1).TrimStart(ch1));
                                if (dstWeightAndQty.Tables[0].Rows.Count > 0)
                                {
                                    newOlderWeight += Convert.ToDecimal(dstWeightAndQty.Tables[0].Rows[0]["od_qty"]) * Convert.ToDecimal(dstWeightAndQty.Tables[0].Rows[0]["od_weight"]);
                                }

                                if (oldOrder != null)
                                {
                                    DataSet dst = GetOrderDetailById(orderId, deletedOrderDetailsIdArray[i].TrimEnd(ch1).TrimStart(ch1));
                                    if (dst.Tables[0].Rows.Count > 0)
                                    {

                                        strSql.Append(" insert into OrderNote(ON_BILLNO,ON_USERCODE,ON_TIME,ON_NOTETYPE,ON_NOTE) values('");
                                        strSql.Append(oldOrder.ORDER_ID);
                                        strSql.Append("','");
                                        strSql.Append(userCode);
                                        strSql.Append("','");
                                        strSql.Append(DateTime.Now);
                                        strSql.Append("','");
                                        strSql.Append(oldOrder.ORDER_STATETYPECODE);
                                        strSql.Append("','修改人:");
                                        strSql.Append(userName);
                                        strSql.Append(" 修改时间:");
                                        strSql.Append(DateTime.Now);
                                        strSql.Append(" 商品代码:");
                                        strSql.Append(dst.Tables[0].Rows[0]["OD_CMDCODE"].ToString());
                                        strSql.Append(" 商品名称:");
                                        strSql.Append(dst.Tables[0].Rows[0]["SPMC"].ToString());
                                        strSql.Append(" 商品颜色:");
                                        strSql.Append(dst.Tables[0].Rows[0]["COLOR"].ToString());
                                        strSql.Append(" 商品尺码:");
                                        strSql.Append(dst.Tables[0].Rows[0]["SIZE"].ToString());
                                        strSql.Append(" 修改后数量:");
                                        strSql.Append(0);
                                        strSql.Append("')|");
                                    }
                                }
                            }

                            foreach (Model.ORDERDETAIL temp in newOrderDetails)
                            {
                                strSql.Append("INSERT INTO ORDERDETAIL([OD_ID],[OD_DETAILID],[OD_CMDCODE],[OD_COLORCODE],[OD_SIZECODE],");
                                strSql.Append("[OD_QTY],[OD_SENDQTY],[OD_SPRICE],[OD_DISC],[OD_APRICE],[OD_SAMT],[OD_AAMT],[OD_NOTE],[OD_FIELD1],");
                                strSql.Append("[OD_FIELD2],[OD_SPPRICE],[OD_GIFT],[OD_ITGBUY],[OD_ITGPRICE],[OD_ITG],[OD_ADDITG],[OD_WEIGHT],");
                                strSql.Append("[OD_OFFER],[OD_OFFERTYPE],[OD_OFFERID],[OD_OFFERNO])VALUES('");
                                strSql.Append(newOrderId);
                                strSql.Append("','");
                                strSql.Append(temp.OD_DETAILID);
                                strSql.Append("','");
                                strSql.Append(temp.OD_CMDCODE);
                                strSql.Append("','");
                                strSql.Append(temp.OD_COLORCODE);
                                strSql.Append("','");
                                strSql.Append(temp.OD_SIZECODE);
                                strSql.Append("',");
                                strSql.Append(temp.OD_QTY);
                                strSql.Append(",");
                                strSql.Append(temp.OD_SENDQTY);
                                strSql.Append(",");
                                strSql.Append(temp.OD_SPPRICE);
                                strSql.Append(",");
                                strSql.Append(temp.OD_DISC);
                                strSql.Append(",");
                                strSql.Append(temp.OD_APRICE);
                                strSql.Append(",");
                                strSql.Append(temp.OD_SAMT);
                                strSql.Append(",");
                                strSql.Append(temp.OD_AAMT);
                                strSql.Append(",");
                                strSql.Append("null,'");
                                strSql.Append(temp.OD_FIELD1);
                                strSql.Append("','");
                                strSql.Append(temp.OD_FIELD2);
                                strSql.Append("',");
                                strSql.Append(temp.OD_SPPRICE);
                                strSql.Append(",");
                                strSql.Append(temp.OD_GIFT);
                                strSql.Append(",");
                                strSql.Append(temp.OD_ITGBUY);
                                strSql.Append(",");
                                strSql.Append(temp.OD_ITGPRICE);
                                strSql.Append(",");
                                strSql.Append(temp.OD_ITG);
                                strSql.Append(",");
                                strSql.Append(temp.OD_ADDITG);
                                strSql.Append(",");
                                strSql.Append(temp.OD_WEIGHT);
                                strSql.Append(",");
                                strSql.Append(temp.OD_OFFER);
                                strSql.Append(",'");
                                strSql.Append(temp.OD_OFFERTYPE);
                                strSql.Append("','");
                                strSql.Append(temp.OD_OFFERID);
                                strSql.Append("','");
                                strSql.Append(temp.OD_OFFERNO);
                                strSql.Append("')| ");

                                newOlderWeight += temp.OD_QTY * temp.OD_WEIGHT;
                            }

                            decimal amountAddToBYZD13 = 0;
                            foreach (Model.ORDERDETAIL temp in newOrderDetails)
                            {
                                strSql.Append(" update SPKCB set SL3=SL3+");
                                strSql.Append(temp.OD_QTY);
                                strSql.Append(" where CKDM='");
                                strSql.Append(ckdm);
                                strSql.Append("' and KWDM='");
                                strSql.Append(kwdm);
                                strSql.Append("' and SPDM='");
                                strSql.Append(temp.OD_CMDCODE);
                                strSql.Append("' and GG1DM='");
                                strSql.Append(temp.OD_COLORCODE);
                                strSql.Append("' and GG2DM='");
                                strSql.Append(temp.OD_SIZECODE);
                                strSql.Append("'|");

                                amountAddToBYZD13 += temp.OD_AAMT;
                            }

                            int pageCount, recordCount;
                            List<Model.ORDERDETAIL> orderDetails = new BLL.ORDERDETAIL().GetList(100, 1, " OD_DETAILID ", false, " and OD_ID='" + orderId + "' and OD_DETAILID in(" + deletedOrderDetailsId.TrimEnd(ch) + ")", out pageCount, out recordCount);
                            if (orderDetails != null && orderDetails.Count > 0)
                            {
                                foreach (Model.ORDERDETAIL temp in orderDetails)
                                {
                                    strSql.Append(" update SPKCB set SL3=SL3+");
                                    strSql.Append(temp.OD_QTY);
                                    strSql.Append(" where CKDM='");
                                    strSql.Append(ckdm);
                                    strSql.Append("' and KWDM='");
                                    strSql.Append(kwdm);
                                    strSql.Append("' and SPDM='");
                                    strSql.Append(temp.OD_CMDCODE);
                                    strSql.Append("' and GG1DM='");
                                    strSql.Append(temp.OD_COLORCODE);
                                    strSql.Append("' and GG2DM='");
                                    strSql.Append(temp.OD_SIZECODE);
                                    strSql.Append("'|");


                                    amountAddToBYZD13 += temp.OD_AAMT;
                                }
                            }

                            if (oldOrder != null && oldOrder.ORDER_DTCODE == "003")
                            {
                                strSql.Append(new BLL.V_VIPSET().BuildUpdateVipSetSqlString(amountAddToBYZD13, 0, 0, oldOrder.ORDER_CUSTOMERCODE));
                            }

                            decimal oldOrderTranCost = new BLL.TRANERCOST().GetTranCost(oldOrder.ORDER_TRANERCODE, oldOrder.ORDER_CITY, oldOrderWeight);
                            decimal newOrderTranCost = new BLL.TRANERCOST().GetTranCost(oldOrder.ORDER_TRANERCODE, oldOrder.ORDER_CITY, newOlderWeight);


                            strSql.Append("update orderdoc set ORDER_TRANCOST=");
                            strSql.Append(oldOrderTranCost);
                            strSql.Append(",ORDER_QTY=(SELECT isnull(SUM(OD_QTY),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'),ORDER_AMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'),ORDER_TAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("')+");
                            strSql.Append(oldOrderTranCost);
                            strSql.Append(",ORDER_AAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("')-ORDER_OFFERAMT+");
                            strSql.Append(oldOrderTranCost);
                            strSql.Append(",ORDER_ITG=(SELECT isnull(SUM(OD_ITG),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'),ORDER_WEIGHT=(SELECT isnull(SUM(OD_QTY*OD_WEIGHT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'),ORDER_TRANERCODE='");
                            strSql.Append(tranerCode);
                            strSql.Append("',ORDER_TRANBILLCODE='");
                            strSql.Append(tranerBillCode);
                            strSql.Append("' where order_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'|");

                            strSql.Append("update orderdoc set ORDER_TRANCOST=");
                            strSql.Append(newOrderTranCost);
                            strSql.Append(",ORDER_QTY=(SELECT isnull(SUM(OD_QTY),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("'),ORDER_AMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("'),ORDER_TAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("')+");
                            strSql.Append(newOrderTranCost);
                            strSql.Append(",ORDER_AAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("')-ORDER_OFFERAMT+");
                            strSql.Append(newOrderTranCost);
                            strSql.Append(",ORDER_ITG=(SELECT isnull(SUM(OD_ITG),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("'),ORDER_WEIGHT=(SELECT isnull(SUM(OD_QTY*OD_WEIGHT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("',ORDER_TRANBILLCODE='");
                            strSql.Append(tranerBillCode);
                            strSql.Append("') where order_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("'|");

                        }

                    }
                    string stringSql = strSql.ToString();
                    char[] charToTrim = new char[] { '|' };
                    string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);
                    return new SQLServerDAL.ORDERDETAIL().UpdateOrderDetailAndOrderMain(sqlCommandArray);

                }
                #endregion
                #region 当没有商品被删除并且订单数量被修改时
                else if (deletedOrderDetailsId == "" && newOrderDetails.Count > 0)
                {
                    StringBuilder strSql = new StringBuilder();

                    Model.ORDERDOC oldOrder = new BLL.ORDERDOC().GetModel(orderId);
                    string userName = (new BLL.USERS().GetModel(userCode)).USERS_NAME;

                    decimal oldOrderWeight = 0.00M;
                    decimal newOlderWeight = 0.00M;

                    foreach (Model.ORDERDETAIL temp in toUpdateOrderDetails)
                    {
                        if (temp.OD_QTY > 0)
                        {
                            strSql.Append(" update ORDERDETAIL set OD_QTY=");
                            strSql.Append(temp.OD_QTY);
                            strSql.Append(", OD_AAMT=");
                            strSql.Append(temp.OD_AAMT);
                            strSql.Append(", OD_SAMT=");
                            strSql.Append(temp.OD_SAMT);
                            strSql.Append(", OD_NOTE='");
                            strSql.Append(temp.OD_NOTE);
                            strSql.Append("', OD_ITG=");
                            strSql.Append(temp.OD_ITG);
                            strSql.Append(", OD_WEIGHT=");
                            strSql.Append(temp.OD_WEIGHT);
                            strSql.Append(" where OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("' and OD_DETAILID='");
                            strSql.Append(temp.OD_DETAILID);
                            strSql.Append("'|");

                            if (oldOrder != null)
                            {
                                strSql.Append(" insert into OrderNote(ON_BILLNO,ON_USERCODE,ON_TIME,ON_NOTETYPE,ON_NOTE) values('");
                                strSql.Append(oldOrder.ORDER_ID);
                                strSql.Append("','");
                                strSql.Append(userCode);
                                strSql.Append("','");
                                strSql.Append(DateTime.Now);
                                strSql.Append("','");
                                strSql.Append(oldOrder.ORDER_STATETYPECODE);
                                strSql.Append("','修改人:");
                                strSql.Append(userName);
                                strSql.Append(" 修改时间:");
                                strSql.Append(DateTime.Now);
                                strSql.Append(" 商品代码:");
                                strSql.Append(temp.OD_CMDCODE);
                                strSql.Append(" 商品名称:");
                                strSql.Append((new BLL.SHANGPIN().GetModel(temp.OD_CMDCODE)).SPMC);
                                strSql.Append(" 商品颜色:");
                                strSql.Append(new BLL.GUIGE1().GetMC(temp.OD_COLORCODE));
                                strSql.Append(" 商品尺码:");
                                strSql.Append(new BLL.GUIGE2().GetMC(temp.OD_SIZECODE));
                                strSql.Append(" 修改后数量:");
                                strSql.Append(temp.OD_QTY);
                                strSql.Append("')|");
                            }

                            oldOrderWeight += temp.OD_WEIGHT * temp.OD_QTY;
                        }
                        else
                        {
                            strSql.Append(" delete from ORDERDETAIL where OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("' and OD_DETAILID='");
                            strSql.Append(temp.OD_DETAILID);
                            strSql.Append("'|");

                            //oldOrderWeight += temp.OD_WEIGHT * temp.OD_QTY;
                        }
                    }

                    if (oldOrder != null)
                    {
                        string s = new SQLServerDAL.ORDERDOC().GetNewOrderID();
                        BLL.Common.ADDLSH("400101");
                        string newOrderId = new SQLServerDAL.ORDERDOC().GetNewOrderID();
                        BLL.Common.ADDLSH("400101");
                        if (newOrderId != null && newOrderId != "")
                        {
                            strSql.Append(" insert into ORDERDOC([ORDER_ID],[ORDER_AID],[ORDER_CUSTOMERCODE],[ORDER_OFCODE],[ORDER_DTCODE],[ORDER_TRANERCODE],");
                            strSql.Append("[ORDER_TRANCOST],[ORDER_COSTNATURE],[ORDER_GETER],[ORDER_GETERPHONE1],[ORDER_GETERPHONE2],[ORDER_GETADDRESS],");
                            strSql.Append("[ORDER_GETPOSTCODE],[ORDER_CREATER],[ORDER_CREATEDATE],[ORDER_STATETYPECODE],[ORDER_FIELD1],[ORDER_FIELD2],");
                            strSql.Append("[ORDER_ITGBUY],[ORDER_ITG],[ORDER_NINV],[ORDER_INVTITLE],[ORDER_OFFERTYPE],[ORDER_OFFERID],[ORDER_NOTE],[ORDER_APAY],[order_Province],[order_city],[order_area],[ORDER_TRANBILLCODE])VALUES('");
                            strSql.Append(newOrderId);
                            strSql.Append("','");
                            strSql.Append(orderId);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_CUSTOMERCODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_OFCODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_DTCODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_TRANERCODE);
                            strSql.Append("',");
                            strSql.Append(oldOrder.ORDER_TRANCOST);
                            strSql.Append(",'");
                            strSql.Append(oldOrder.ORDER_COSTNATURE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETER);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETERPHONE1);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETERPHONE2);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETADDRESS);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_GETPOSTCODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_CREATER);
                            strSql.Append("','");
                            strSql.Append(DateTime.Now);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_STATETYPECODE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_FIELD1);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_FIELD2);
                            strSql.Append("',");
                            strSql.Append(oldOrder.ORDER_ITGBUY);
                            strSql.Append(",");
                            strSql.Append(oldOrder.ORDER_ITG);
                            strSql.Append(",");
                            strSql.Append(oldOrder.ORDER_NINV);
                            strSql.Append(",'");
                            strSql.Append(oldOrder.ORDER_INVTITLE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_OFFERTYPE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_OFFERID);
                            strSql.Append("','差异订单',");
                            strSql.Append(oldOrder.ORDER_APAY);
                            strSql.Append(",'");
                            strSql.Append(oldOrder.ORDER_PROVINCE);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_CITY);
                            strSql.Append("','");
                            strSql.Append(oldOrder.ORDER_AREA);
                            strSql.Append("','");
                            strSql.Append(tranerBillCode);
                            strSql.Append("')|");


                            foreach (Model.ORDERDETAIL temp in newOrderDetails)
                            {
                                strSql.Append("INSERT INTO ORDERDETAIL([OD_ID],[OD_DETAILID],[OD_CMDCODE],[OD_COLORCODE],[OD_SIZECODE],");
                                strSql.Append("[OD_QTY],[OD_SENDQTY],[OD_SPRICE],[OD_DISC],[OD_APRICE],[OD_SAMT],[OD_AAMT],[OD_NOTE],[OD_FIELD1],");
                                strSql.Append("[OD_FIELD2],[OD_SPPRICE],[OD_GIFT],[OD_ITGBUY],[OD_ITGPRICE],[OD_ITG],[OD_ADDITG],[OD_WEIGHT],");
                                strSql.Append("[OD_OFFER],[OD_OFFERTYPE],[OD_OFFERID],[OD_OFFERNO])VALUES('");
                                strSql.Append(newOrderId);
                                strSql.Append("','");
                                strSql.Append(temp.OD_DETAILID);
                                strSql.Append("','");
                                strSql.Append(temp.OD_CMDCODE);
                                strSql.Append("','");
                                strSql.Append(temp.OD_COLORCODE);
                                strSql.Append("','");
                                strSql.Append(temp.OD_SIZECODE);
                                strSql.Append("',");
                                strSql.Append(temp.OD_QTY);
                                strSql.Append(",");
                                strSql.Append(temp.OD_SENDQTY);
                                strSql.Append(",");
                                strSql.Append(temp.OD_SPRICE);
                                strSql.Append(",");
                                strSql.Append(temp.OD_DISC);
                                strSql.Append(",");
                                strSql.Append(temp.OD_APRICE);
                                strSql.Append(",");
                                strSql.Append(temp.OD_SAMT);
                                strSql.Append(",");
                                strSql.Append(temp.OD_AAMT);
                                strSql.Append(",");
                                strSql.Append("null,'");
                                strSql.Append(temp.OD_FIELD1);
                                strSql.Append("','");
                                strSql.Append(temp.OD_FIELD2);
                                strSql.Append("',");
                                strSql.Append(temp.OD_SPPRICE);
                                strSql.Append(",");
                                strSql.Append(temp.OD_GIFT);
                                strSql.Append(",");
                                strSql.Append(temp.OD_ITGBUY);
                                strSql.Append(",");
                                strSql.Append(temp.OD_ITGPRICE);
                                strSql.Append(",");
                                strSql.Append(temp.OD_ITG);
                                strSql.Append(",");
                                strSql.Append(temp.OD_ADDITG);
                                strSql.Append(",");
                                strSql.Append(temp.OD_WEIGHT);
                                strSql.Append(",");
                                strSql.Append(temp.OD_OFFER);
                                strSql.Append(",'");
                                strSql.Append(temp.OD_OFFERTYPE);
                                strSql.Append("','");
                                strSql.Append(temp.OD_OFFERID);
                                strSql.Append("','");
                                strSql.Append(temp.OD_OFFERNO);
                                strSql.Append("')| ");

                                newOlderWeight += temp.OD_QTY * temp.OD_WEIGHT;
                            }

                            decimal amountAddToBYZD13 = 0;
                            foreach (Model.ORDERDETAIL temp in newOrderDetails)
                            {
                                strSql.Append(" update SPKCB set SL3=SL3+");
                                strSql.Append(temp.OD_QTY);
                                strSql.Append(" where CKDM='");
                                strSql.Append(ckdm);
                                strSql.Append("' and KWDM='");
                                strSql.Append(kwdm);
                                strSql.Append("' and SPDM='");
                                strSql.Append(temp.OD_CMDCODE);
                                strSql.Append("' and GG1DM='");
                                strSql.Append(temp.OD_COLORCODE);
                                strSql.Append("' and GG2DM='");
                                strSql.Append(temp.OD_SIZECODE);
                                strSql.Append("'|");


                                amountAddToBYZD13 += temp.OD_AAMT;
                            }

                            if (oldOrder != null && oldOrder.ORDER_DTCODE == "003")
                            {
                                strSql.Append(new BLL.V_VIPSET().BuildUpdateVipSetSqlString(amountAddToBYZD13, 0, 0, oldOrder.ORDER_CUSTOMERCODE));
                            }

                            decimal oldOrderTranCost = new BLL.TRANERCOST().GetTranCost(oldOrder.ORDER_TRANERCODE, oldOrder.ORDER_CITY, oldOrderWeight);
                            decimal newOrderTranCost = new BLL.TRANERCOST().GetTranCost(oldOrder.ORDER_TRANERCODE, oldOrder.ORDER_CITY, newOlderWeight);


                            strSql.Append("update orderdoc set ORDER_TRANCOST=");
                            strSql.Append(oldOrderTranCost);
                            strSql.Append(",ORDER_QTY=(SELECT isnull(SUM(OD_QTY),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'),ORDER_AMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'),ORDER_TAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("')+");
                            strSql.Append(oldOrderTranCost);
                            strSql.Append(",ORDER_AAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("')-ORDER_OFFERAMT+");
                            strSql.Append(oldOrderTranCost);
                            strSql.Append(",ORDER_ITG=(SELECT isnull(SUM(OD_ITG),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'),ORDER_WEIGHT=(SELECT isnull(SUM(OD_QTY*OD_WEIGHT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'),ORDER_TRANERCODE='");
                            strSql.Append(tranerCode);
                            strSql.Append("',ORDER_TRANBILLCODE='");
                            strSql.Append(tranerBillCode);
                            strSql.Append("' where order_ID='");
                            strSql.Append(orderId);
                            strSql.Append("'|");

                            strSql.Append("update orderdoc set ORDER_TRANCOST=");
                            strSql.Append(newOrderTranCost);
                            strSql.Append(",ORDER_QTY=(SELECT isnull(SUM(OD_QTY),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("'),ORDER_AMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("'),ORDER_TAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("')+");
                            strSql.Append(newOrderTranCost);
                            strSql.Append(",ORDER_AAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("')-ORDER_OFFERAMT+");
                            strSql.Append(newOrderTranCost);
                            strSql.Append(",ORDER_ITG=(SELECT isnull(SUM(OD_ITG),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("'),ORDER_WEIGHT=(SELECT isnull(SUM(OD_QTY*OD_WEIGHT),0) FROM orderDETAIL WHERE OD_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("',ORDER_TRANBILLCODE='");
                            strSql.Append(tranerBillCode);
                            strSql.Append("') where order_ID='");
                            strSql.Append(newOrderId);
                            strSql.Append("'|");
                        }

                    }
                    string stringSql = strSql.ToString();
                    char[] charToTrim = new char[] { '|' };
                    string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);
                    return new SQLServerDAL.ORDERDETAIL().UpdateOrderDetailAndOrderMain(sqlCommandArray);
                }
                #endregion
                #region 当没有商品被删除并且订单数量没有被修改时
                else
                {
                    StringBuilder strSql = new StringBuilder();

                    strSql.Append("update orderdoc set ORDER_QTY=(SELECT isnull(SUM(OD_QTY),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'),ORDER_AMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'),ORDER_TAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("')+ORDER_TRANCOST,ORDER_AAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("')-ORDER_OFFERAMT+ORDER_TRANCOST,");
                    strSql.Append("ORDER_ITG=(SELECT isnull(SUM(OD_ITG),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'),ORDER_WEIGHT=(SELECT isnull(SUM(OD_QTY*OD_WEIGHT),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'),ORDER_TRANERCODE='");
                    strSql.Append(tranerCode);
                    strSql.Append("',ORDER_TRANBILLCODE='");
                    strSql.Append(tranerBillCode);
                    strSql.Append("' where order_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'|");

                    foreach (Model.ORDERDETAIL temp in toUpdateOrderDetails)
                    {
                        strSql.Append(" update ORDERDETAIL set OD_NOTE='");
                        strSql.Append(temp.OD_NOTE);
                        strSql.Append("' where OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("' and OD_DETAILID='");
                        strSql.Append(temp.OD_DETAILID);
                        strSql.Append("'|");
                    }



                    string stringSql = strSql.ToString();
                    char[] charToTrim = new char[] { '|' };
                    string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);
                    return new SQLServerDAL.ORDERDETAIL().UpdateOrderDetailAndOrderMain(sqlCommandArray);
                }
                #endregion
            }
            #endregion
            #region 不生成差异订单

            else
            {
                #region 当有商品被删除时并且订单数量被修改时
                if (deletedOrderDetailsId != "" && newOrderDetails.Count >= 0)
                {
                    StringBuilder strSql = new StringBuilder();

                    Model.ORDERDOC oldOrder = new BLL.ORDERDOC().GetModel(orderId);
                    string userName = (new BLL.USERS().GetModel(userCode)).USERS_NAME;

                    decimal orderWeight = 0.00M;

                    foreach (Model.ORDERDETAIL temp in toUpdateOrderDetails)
                    {
                        if (temp.OD_QTY > 0)
                        {
                            strSql.Append(" update ORDERDETAIL set OD_QTY=");
                            strSql.Append(temp.OD_QTY);
                            strSql.Append(", OD_AAMT=");
                            strSql.Append(temp.OD_AAMT);
                            strSql.Append(", OD_SAMT=");
                            strSql.Append(temp.OD_SAMT);
                            strSql.Append(", OD_NOTE='");
                            strSql.Append(temp.OD_NOTE);
                            strSql.Append("', OD_ITG=");
                            strSql.Append(temp.OD_ITG);
                            strSql.Append(", OD_WEIGHT=");
                            strSql.Append(temp.OD_WEIGHT);
                            strSql.Append(" where OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("' and OD_DETAILID='");
                            strSql.Append(temp.OD_DETAILID);
                            strSql.Append("'|");

                            if (oldOrder != null)
                            {
                                strSql.Append(" insert into OrderNote(ON_BILLNO,ON_USERCODE,ON_TIME,ON_NOTETYPE,ON_NOTE) values('");
                                strSql.Append(oldOrder.ORDER_ID);
                                strSql.Append("','");
                                strSql.Append(userCode);
                                strSql.Append("','");
                                strSql.Append(DateTime.Now);
                                strSql.Append("','");
                                strSql.Append(oldOrder.ORDER_STATETYPECODE);
                                strSql.Append("','修改人:");
                                strSql.Append(userName);
                                strSql.Append(" 修改时间:");
                                strSql.Append(DateTime.Now);
                                strSql.Append(" 商品代码:");
                                strSql.Append(temp.OD_CMDCODE);
                                strSql.Append(" 商品名称:");
                                strSql.Append((new BLL.SHANGPIN().GetModel(temp.OD_CMDCODE)).SPMC);
                                strSql.Append(" 商品颜色:");
                                strSql.Append(new BLL.GUIGE1().GetMC(temp.OD_COLORCODE));
                                strSql.Append(" 商品尺码:");
                                strSql.Append(new BLL.GUIGE2().GetMC(temp.OD_SIZECODE));
                                strSql.Append(" 修改后数量:");
                                strSql.Append(temp.OD_QTY);
                                strSql.Append("')|");
                            }

                            orderWeight += temp.OD_WEIGHT * temp.OD_QTY;
                        }
                        else
                        {
                            strSql.Append(" delete from ORDERDETAIL where OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("' and OD_DETAILID='");
                            strSql.Append(temp.OD_DETAILID);
                            strSql.Append("'|");

                            //orderWeight += temp.OD_WEIGHT * temp.OD_QTY;
                        }
                    }

                    if (oldOrder != null)
                    {
                        char[] ch = new char[] { ',' };
                        char[] ch1 = new char[] { '\'' };
                        string[] deletedOrderDetailsIdArray = deletedOrderDetailsId.TrimEnd(ch).Split(',');
                        for (int i = 0; i < deletedOrderDetailsIdArray.Length; i++)
                        {
                            strSql.Append("delete from ORDERDETAIL where OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("' and OD_DETAILID=");
                            strSql.Append(deletedOrderDetailsIdArray[i]);
                            strSql.Append(" |");



                            if (oldOrder != null)
                            {
                                DataSet dst = GetOrderDetailById(orderId, deletedOrderDetailsIdArray[i].TrimEnd(ch1).TrimStart(ch1));
                                if (dst.Tables[0].Rows.Count > 0)
                                {
                                    strSql.Append(" insert into OrderNote(ON_BILLNO,ON_USERCODE,ON_TIME,ON_NOTETYPE,ON_NOTE) values('");
                                    strSql.Append(oldOrder.ORDER_ID);
                                    strSql.Append("','");
                                    strSql.Append(userCode);
                                    strSql.Append("','");
                                    strSql.Append(DateTime.Now);
                                    strSql.Append("','");
                                    strSql.Append(oldOrder.ORDER_STATETYPECODE);
                                    strSql.Append("','修改人:");
                                    strSql.Append(userName);
                                    strSql.Append(" 修改时间:");
                                    strSql.Append(DateTime.Now);
                                    strSql.Append(" 商品代码:");
                                    strSql.Append(dst.Tables[0].Rows[0]["OD_CMDCODE"].ToString());
                                    strSql.Append(" 商品名称:");
                                    strSql.Append(dst.Tables[0].Rows[0]["SPMC"].ToString());
                                    strSql.Append(" 商品颜色:");
                                    strSql.Append(dst.Tables[0].Rows[0]["COLOR"].ToString());
                                    strSql.Append(" 商品尺码:");
                                    strSql.Append(dst.Tables[0].Rows[0]["SIZE"].ToString());
                                    strSql.Append(" 修改后数量:");
                                    strSql.Append(0);
                                    strSql.Append("')|");
                                }
                            }
                        }


                        decimal amountAddToBYZD13 = 0;
                        foreach (Model.ORDERDETAIL temp in newOrderDetails)
                        {
                            strSql.Append(" update SPKCB set SL3=SL3+");
                            strSql.Append(temp.OD_QTY);
                            strSql.Append(" where CKDM='");
                            strSql.Append(ckdm);
                            strSql.Append("' and KWDM='");
                            strSql.Append(kwdm);
                            strSql.Append("' and SPDM='");
                            strSql.Append(temp.OD_CMDCODE);
                            strSql.Append("' and GG1DM='");
                            strSql.Append(temp.OD_COLORCODE);
                            strSql.Append("' and GG2DM='");
                            strSql.Append(temp.OD_SIZECODE);
                            strSql.Append("'|");


                            amountAddToBYZD13 += temp.OD_AAMT;
                        }

                        int pageCount, recordCount;
                        List<Model.ORDERDETAIL> orderDetails = new BLL.ORDERDETAIL().GetList(100, 1, " OD_DETAILID ", false, " and OD_ID='" + orderId + "' and OD_DETAILID in(" + deletedOrderDetailsId.TrimEnd(ch) + ")", out pageCount, out recordCount);
                        if (orderDetails != null && orderDetails.Count > 0)
                        {
                            foreach (Model.ORDERDETAIL temp in orderDetails)
                            {
                                strSql.Append(" update SPKCB set SL3=SL3+");
                                strSql.Append(temp.OD_QTY);
                                strSql.Append(" where CKDM='");
                                strSql.Append(ckdm);
                                strSql.Append("' and KWDM='");
                                strSql.Append(kwdm);
                                strSql.Append("' and SPDM='");
                                strSql.Append(temp.OD_CMDCODE);
                                strSql.Append("' and GG1DM='");
                                strSql.Append(temp.OD_COLORCODE);
                                strSql.Append("' and GG2DM='");
                                strSql.Append(temp.OD_SIZECODE);
                                strSql.Append("'|");



                                amountAddToBYZD13 += temp.OD_AAMT;
                            }
                        }

                        if (oldOrder != null && oldOrder.ORDER_DTCODE == "003")
                        {
                            strSql.Append(new BLL.V_VIPSET().BuildUpdateVipSetSqlString(amountAddToBYZD13, 0, 0, oldOrder.ORDER_CUSTOMERCODE));
                        }

                        decimal orderTranCost = new BLL.TRANERCOST().GetTranCost(oldOrder.ORDER_TRANERCODE, oldOrder.ORDER_CITY, orderWeight);

                        strSql.Append("update orderdoc set ORDER_TRANCOST=");
                        strSql.Append(orderTranCost);
                        strSql.Append(",ORDER_QTY=(SELECT isnull(SUM(OD_QTY),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'),ORDER_AMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'),ORDER_TAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("')+");
                        strSql.Append(orderTranCost);
                        strSql.Append(",ORDER_AAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("')-ORDER_OFFERAMT+");
                        strSql.Append(orderTranCost);
                        strSql.Append(",ORDER_ITG=(SELECT isnull(SUM(OD_ITG),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'),ORDER_WEIGHT=(SELECT isnull(SUM(OD_QTY*OD_WEIGHT),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'),ORDER_TRANERCODE='");
                        strSql.Append(tranerCode);
                        strSql.Append("',ORDER_TRANBILLCODE='");
                        strSql.Append(tranerBillCode);
                        strSql.Append("' where order_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'|");
                    }
                    string stringSql = strSql.ToString();
                    char[] charToTrim = new char[] { '|' };
                    string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);
                    return new SQLServerDAL.ORDERDETAIL().UpdateOrderDetailAndOrderMain(sqlCommandArray);

                }
                #endregion
                #region 当没有商品被删除并且订单数量被修改时
                else if (deletedOrderDetailsId == "" && newOrderDetails.Count > 0)
                {
                    StringBuilder strSql = new StringBuilder();

                    Model.ORDERDOC oldOrder = new BLL.ORDERDOC().GetModel(orderId);
                    string userName = (new BLL.USERS().GetModel(userCode)).USERS_NAME;

                    decimal orderWeight = 0.00M;

                    foreach (Model.ORDERDETAIL temp in toUpdateOrderDetails)
                    {
                        if (temp.OD_QTY > 0)
                        {
                            strSql.Append(" update ORDERDETAIL set OD_QTY=");
                            strSql.Append(temp.OD_QTY);
                            strSql.Append(", OD_AAMT=");
                            strSql.Append(temp.OD_AAMT);
                            strSql.Append(", OD_SAMT=");
                            strSql.Append(temp.OD_SAMT);
                            strSql.Append(", OD_NOTE='");
                            strSql.Append(temp.OD_NOTE);
                            strSql.Append("', OD_ITG=");
                            strSql.Append(temp.OD_ITG);
                            strSql.Append(", OD_WEIGHT=");
                            strSql.Append(temp.OD_WEIGHT);
                            strSql.Append(" where OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("' and OD_DETAILID='");
                            strSql.Append(temp.OD_DETAILID);
                            strSql.Append("'|");


                            if (oldOrder != null)
                            {
                                strSql.Append(" insert into OrderNote(ON_BILLNO,ON_USERCODE,ON_TIME,ON_NOTETYPE,ON_NOTE) values('");
                                strSql.Append(oldOrder.ORDER_ID);
                                strSql.Append("','");
                                strSql.Append(userCode);
                                strSql.Append("','");
                                strSql.Append(DateTime.Now);
                                strSql.Append("','");
                                strSql.Append(oldOrder.ORDER_STATETYPECODE);
                                strSql.Append("','修改人:");
                                strSql.Append(userName);
                                strSql.Append(" 修改时间:");
                                strSql.Append(DateTime.Now);
                                strSql.Append(" 商品代码:");
                                strSql.Append(temp.OD_CMDCODE);
                                strSql.Append(" 商品名称:");
                                strSql.Append((new BLL.SHANGPIN().GetModel(temp.OD_CMDCODE)).SPMC);
                                strSql.Append(" 商品颜色:");
                                strSql.Append(new BLL.GUIGE1().GetMC(temp.OD_COLORCODE));
                                strSql.Append(" 商品尺码:");
                                strSql.Append(new BLL.GUIGE2().GetMC(temp.OD_SIZECODE));
                                strSql.Append(" 修改后数量:");
                                strSql.Append(temp.OD_QTY);
                                strSql.Append("')|");
                            }

                            orderWeight += temp.OD_WEIGHT * temp.OD_QTY;
                        }
                        else
                        {
                            strSql.Append(" delete from ORDERDETAIL where OD_ID='");
                            strSql.Append(orderId);
                            strSql.Append("' and OD_DETAILID='");
                            strSql.Append(temp.OD_DETAILID);
                            strSql.Append("'|");
                        }
                    }

                    if (oldOrder != null)
                    {
                        decimal amountAddToBYZD13 = 0;
                        foreach (Model.ORDERDETAIL temp in newOrderDetails)
                        {
                            strSql.Append(" update SPKCB set SL3=SL3+");
                            strSql.Append(temp.OD_QTY);
                            strSql.Append(" where CKDM='");
                            strSql.Append(ckdm);
                            strSql.Append("' and KWDM='");
                            strSql.Append(kwdm);
                            strSql.Append("' and SPDM='");
                            strSql.Append(temp.OD_CMDCODE);
                            strSql.Append("' and GG1DM='");
                            strSql.Append(temp.OD_COLORCODE);
                            strSql.Append("' and GG2DM='");
                            strSql.Append(temp.OD_SIZECODE);
                            strSql.Append("'|");


                            amountAddToBYZD13 += temp.OD_AAMT;
                        }

                        if (oldOrder != null && oldOrder.ORDER_DTCODE == "003")
                        {
                            strSql.Append(new BLL.V_VIPSET().BuildUpdateVipSetSqlString(amountAddToBYZD13, 0, 0, oldOrder.ORDER_CUSTOMERCODE));
                        }

                        decimal orderTranCost = new BLL.TRANERCOST().GetTranCost(oldOrder.ORDER_TRANERCODE, oldOrder.ORDER_CITY, orderWeight);

                        strSql.Append("update orderdoc set ORDER_TRANCOST=");
                        strSql.Append(orderTranCost);
                        strSql.Append(",ORDER_QTY=(SELECT isnull(SUM(OD_QTY),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'),ORDER_AMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'),ORDER_TAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("')+");
                        strSql.Append(orderTranCost);
                        strSql.Append(",ORDER_AAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("')-ORDER_OFFERAMT+");
                        strSql.Append(orderTranCost);
                        strSql.Append(",ORDER_ITG=(SELECT isnull(SUM(OD_ITG),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'),ORDER_WEIGHT=(SELECT isnull(SUM(OD_QTY*OD_WEIGHT),0) FROM orderDETAIL WHERE OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'),ORDER_TRANERCODE='");
                        strSql.Append(tranerCode);
                        strSql.Append("',ORDER_TRANBILLCODE='");
                        strSql.Append(tranerBillCode);
                        strSql.Append("' where order_ID='");
                        strSql.Append(orderId);
                        strSql.Append("'|");


                    }
                    string stringSql = strSql.ToString();
                    char[] charToTrim = new char[] { '|' };
                    string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);
                    return new SQLServerDAL.ORDERDETAIL().UpdateOrderDetailAndOrderMain(sqlCommandArray);
                }
                #endregion
                #region 当没有商品被删除并且订单数量没有被修改时
                else
                {
                    StringBuilder strSql = new StringBuilder();

                    strSql.Append("update orderdoc set ORDER_QTY=(SELECT isnull(SUM(OD_QTY),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'),ORDER_AMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'),ORDER_TAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("')+ORDER_TRANCOST,ORDER_AAMT=(SELECT isnull(SUM(OD_AAMT),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("')-ORDER_OFFERAMT+ORDER_TRANCOST,");
                    strSql.Append("ORDER_ITG=(SELECT isnull(SUM(OD_ITG),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'),ORDER_WEIGHT=(SELECT isnull(SUM(OD_QTY*OD_WEIGHT),0) FROM orderDETAIL WHERE OD_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'),ORDER_TRANERCODE='");
                    strSql.Append(tranerCode);
                    strSql.Append("',ORDER_TRANBILLCODE='");
                    strSql.Append(tranerBillCode);
                    strSql.Append("' where order_ID='");
                    strSql.Append(orderId);
                    strSql.Append("'|");

                    foreach (Model.ORDERDETAIL temp in toUpdateOrderDetails)
                    {
                        strSql.Append(" update ORDERDETAIL set OD_NOTE='");
                        strSql.Append(temp.OD_NOTE);
                        strSql.Append("' where OD_ID='");
                        strSql.Append(orderId);
                        strSql.Append("' and OD_DETAILID='");
                        strSql.Append(temp.OD_DETAILID);
                        strSql.Append("'|");
                    }



                    string stringSql = strSql.ToString();
                    char[] charToTrim = new char[] { '|' };
                    string[] sqlCommandArray = stringSql.TrimEnd(charToTrim).Split(charToTrim);
                    return new SQLServerDAL.ORDERDETAIL().UpdateOrderDetailAndOrderMain(sqlCommandArray);
                }
                #endregion
            }
            #endregion

        }

    }
}
